<?php


namespace plugin\exportcenter\app\strategy;

use app\model\order\Order;
use plugin\exportcenter\app\logic\Export;
use plugin\exportcenter\app\service\CsvExport;

class OrderExport extends Export
{
    public $action = "order";

    public function export($id, $data)
    {
        $export_params = json_decode($data['export_params'], true);
        $where = $export_params['where'];

        $query = Order::where($where);
        $query->with([
            'user' => function ($query) {
                $query->field([
                    "id",
                    "username",
                    "nickname",
                ]);
            },
            'address' => function ($query) {
                $query->field([
                    "order_id",
                    "user_name",
                    "phone",
                    "province",
                    "city",
                    "county",
                    "street",
                    "detail",
                ]);
            },
            'detail' => function ($query) {
                $query->field([
                    "order_id",
                    "goods_name",
                    "price",
                    "logo",
                    "sku_id",
                    "sku_name",
                    "num",
                ]);
            },
        ]);

        $header = [
            "订单号" => "order_no",
            "订单类型" => "type_text",
            "购买人" => "user.nickname",
            "收货人" => "address.user_name",
            "收货人手机号" => "address.phone",
            "收货地址邮编" => "address.post_code",
            "收货地址" => function ($item) {
                return sprintf("%s%s%s%s%s",
                    $item['address']['province'] ?? "",
                    $item['address']['city'] ?? "",
                    $item['address']['county'] ?? "",
                    $item['address']['street'] ?? "",
                    $item['address']['detail'] ?? ""
                );
            },
            "商品信息" => "sku_info",
            "实际支付" => "pay_price",
            "支付状态" => "pay_status_text",
            "订单状态" => "status_text",
            "是否退货" => function ($item) {
                return $item['refund_status'] > 1 ? "是" : "否";
            },
            "退货状态" => function ($item) {
                return $item['refund_status'] > 1 ? $item['refund_status_text'] : "";
            },
            "下单时间" => "create_time",
            "支付时间" => "pay_time",
        ];
        $csv = new CsvExport;
        $csv->setHeader($header);

        $query->chunk(2000, function ($res) use ($csv) {
            $res = $res->toArray();
            array_walk($res, function (&$v) {
                $sku_info = [];
                if (isset($v['detail'])) {
                    foreach ($v['detail'] as $item) {
                        $sku_info[] = sprintf("%s[%s] %sX%s", $item['goods_name'], $item['sku_name'], $item['price'], $item['num']);
                    }
                }
                $v['sku_info'] = implode(';', $sku_info);
            });
            $csv->batchExport($res);
        }, 'id', 'desc');
        //上传到oss
        return $this->uploadOss($csv->getPath());
    }
}